<?php
namespace app\system\controller;

use app\system\model\NewClassModel;

/**
 * 文章分类
 * 
 **/

class Newclass extends Common
{

    /**
     * 列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
		$model = new NewClassModel();
        $list = $model->order( 'sort desc,id desc' )->select();
        $this->assign('list', classsorting($list) );
        return $this->fetch() ;
    }

    /**
     * 添加
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        $model = new NewClassModel();
    	if( isAjaxPost() ) {
           	$data = input('post.');
			$validate = new \app\system\validate\Newclass();
            if(!$validate->check($data)) {
                return $this->jsonError($validate->getError());
            }

            $id = $data['id'];
            unset($data['id']);
            if($id > 0) {
                if($model->where('id', $id)->update($data) !== false) {
                    return $this->jsonSuccess('修改成功');
                } else {
                    return $this->jsonError('修改失败');
                }
            } else {
                $data['time'] = time();
                if($model->insert($data)) {
                    return $this->jsonSuccess('添加成功');
                } else {
                    return $this->jsonError('添加失败');
                }
            }
        }

        $id = input('id', 0, 'intval');
        if($id > 0) {
            $data = $model->get($id)->toArray();
            $Newclass = $model->order( 'sort desc' )->select();

            $nextList = classification( $Newclass, $id ); // 取得所有下级，下级不能选中
            $nextList[] = $id; // 自己也不能选中
            foreach( $Newclass as $k => $v ) { // 下级不能选中
                if(in_array( $v['id'], $nextList )) {
                    $Newclass[$k]['disabled'] = true;
                } else {
                    $Newclass[$k]['disabled'] = false;
                }
            }
            $Newclass = classsorting( $Newclass );

            $this->assign( 'data', $data );
            $this->assign( 'list', $Newclass );
        } else {
            $list = $model->order( 'sort desc,id desc' )->select();
            $this->assign('list', classsorting($list) );
        }
        return $this->fetch();
    }

    /**
     * 修改分类
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function save()
    {
        $newsclass_model = new NewClassModel();
		if(request()->isPost()) {
			
			$data = input( 'post.' );
			$validate = new \app\system\validate\Newclass();
			! $validate->check( $data ) && $this->error( $validate->getError() );
			
			$saveId = $newsclass_model->update( $data );
			$saveId === false && $this->error( '操作失败！' );
			
			$this->success( '修改成功！', url( 'index' ), '', 1 );
		} else {
			
			$id = input( 'param.id' );
			! $id && $this->error( '参数错误！' );
			
			$data = $newsclass_model->get( $id )->toArray();
			$Newclass = $newsclass_model->order( 'sort desc' )->select();
			
			$nextList = classification( $Newclass, $id ); // 取得所有下级，下级不能选中
			$nextList[] = $id; // 自己也不能选中
			foreach( $Newclass as $k => $v ) { // 下级不能选中
				if(in_array( $v['id'], $nextList )) {
					$Newclass[$k]['disabled'] = true;
				} else {
					$Newclass[$k]['disabled'] = false;
				}
			}
			$Newclass = classsorting( $Newclass );
			
			$this->assign( 'data', $data );
			$this->assign( 'list', $Newclass );
			return $this->fetch();
		}

	}

    /**
     * 排序
     * */
    public function saveSort()
    {
        !isAjaxPost() && die;
        $model = new NewClassModel();
        $sort = input('post.');
        foreach($sort as $k => $v) {
            $model->where('id', $k)->update(['sort' => $v]);
        }
        return $this->jsonSuccess('修改成功');
    }

    /**
     * 删除
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
	public function del()
	{
        if(isAjaxPost()) {
            $id = input('param.id');
            if($id <= 0) {
                return $this->jsonError('参数有误');
            }

            $model = new NewClassModel();
            $Newclass = $model->order('sort desc')->select();

            $delArr = classification($Newclass, $id); // 取得所有下级一起删除
            $delArr[] = $id; // 包括自己

            if($model->where(['id'=>['in', $delArr]])->delete() !== false) {
                return $this->jsonSuccess('删除成功');
            } else {
                return $this->jsonError('删除失败');
            }
        }
	}
   
    
}